<template>
  <!-- 主体区域 -->
  <section id="app">
    <TodoHeader  @add="addContent"></TodoHeader>
    <TodoMain :list="list" @delTodoname="delItem"></TodoMain>
    <TodoFooter :list="list" @clearContent="clearItem"></TodoFooter>
  </section>
</template>

<script>
import TodoFooter from './components/TodoFooter.vue';
import TodoMain from './components/TodoMain.vue';
import TodoHeader from './components/TodoHeader.vue';
export default {
  components:{
    TodoHeader,
    TodoMain,
    TodoFooter
  },
  data (){
    return {
      list:JSON.parse(localStorage.getItem('list'))  || [
        {id:1,name:'打篮球'},
        {id:2,name:'跳绳'},
        {id:3,name:'游泳'}
      ]
    }
  },
  methods:{
    // ! 添加功能
    addContent(value){
      this.list.unshift({
        id:+new Date(),
        name:value
      })
    },
    // ! 删除功能
    delItem(id){
      // ! 父组件拿到子组件传递过来的id 筛选数组
      this.list = this.list.filter(item => item.id !== id)
    },
    // ! 清空功能
    clearItem(){
      this.list = []
    }
  },
  // ! 持久化本地存储
  watch:{
    list:{
      deep:true,
      handler(newValue){
        localStorage.setItem('list',JSON.stringify(newValue))
      }
    }
  }
}
</script>

<style>

</style>
